/**
 * *************************************
 * Copyright(c)2016 Dingdang Yixia 
 * All Rights Reserved.
 *
 * @File: FaceosNotifyServiceImpl.java
 * @Package: com.ddyx.service.impl
 * @Description:
 * 
 * @Version: V1.0
 * @Author: Yidong.Xiang
 * @Date: 2017年8月27日 下午7:27:14
 * 
 * *************************************
 */
package com.ddyx.service.impl;

import java.io.File;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.TextMessage;

import org.springframework.stereotype.Service;

import com.ddyx.common.util.ImageFileUtil;
import com.ddyx.entity.FaceNotice;
import com.ddyx.entity.Customer;
import com.ddyx.entity.EventLog;
import com.ddyx.entity.EventStorelog;
import com.ddyx.entity.FaceSnapshot;
import com.ddyx.mapper.FaceNoticeMapper;
import com.ddyx.mapper.CustomerMapper;
import com.ddyx.mapper.EventLogMapper;
import com.ddyx.mapper.EventStorelogMapper;
import com.ddyx.mapper.FaceSnapshotMapper;
import com.ddyx.service.IFaceNotifyService;
import com.ddyx.service.base.BaseNodifyService;

/**
 * @ClassName: FaceppNotifyServiceImpl.java
 * @Module: Face++人脸抓拍机集成模块
 * @Description: 从MQ服务器队列中获人脸识别图片
 * 
 * @author: Yidong.Xiang
 * @date: 2017年8月27日 下午7:27:14
 * 
 */
@Service("snapFaceNotifyService")
public class SnapFaceNotifyServiceImpl extends BaseNodifyService implements IFaceNotifyService {

	@Resource
	FaceNoticeMapper faceNoticeMapper;
	@Resource
	FaceSnapshotMapper faceSnapshotMapper;
	@Resource 
	CustomerMapper customerMapper;
	@Resource
	EventLogMapper eventLogMapper;
	@Resource
	EventStorelogMapper eventStorelogMapper;
	
	static String webappRoot = System.getProperty("webapp.root");

	/* (non-Javadoc)
	 * @see com.ddyx.service.base.BaseNodifyService#doMessage(javax.jms.MapMessage)
	 */
	@Override
	public void doMessage(MapMessage mapMessage) {
		if(mapMessage==null)return;
		
		try {
//			String ipcId = mapMessage.getString("IPC_ID");//网络摄像头ID
//			String deviceId = mapMessage.getString("DeviceId");//摄像头主机设备ID
//			int blackId = mapMessage.getInt("BlackID");//识别名单ID
//			byte[] faceImage = mapMessage.getBytes("FaceImg");//人脸头像图片
//			byte[] fullImage = mapMessage.getBytes("FullImg");//人脸全景图片
//			float score = mapMessage.getFloat("Score");//比对分值
//			String searchResult = mapMessage.getString("SearchResult");//识别结果
//			String collectTime = mapMessage.getString("CollectTime");//抓拍时间
//			String imageUuid = MD5FileUtil.getMD5String(faceImage);//生成人脸头像MD5编码
//			String faceSessionNo = mapMessage.getString("FaceId");//人脸识别编码，一个人识别一次的标识码
			
			String snapId =mapMessage.getString("snap_id");
			String ipcId = mapMessage.getString("ipc_id");
			byte[] faceImage = mapMessage.getBytes("face_image");
			byte[]fullImage = mapMessage.getBytes("full_image");
			int faceCount = mapMessage.getInt("face_count");
			String snapTime = mapMessage.getString("snap_time");
			long msec = mapMessage.getLong("msec");
			String imageId = mapMessage.getString("image_id");
			String targetFacetoken = mapMessage.getString("target_facetoken");
			String faceToken = mapMessage.getString("face_token");
			String imageMd5 = mapMessage.getString("image_md5");
			float confidence = mapMessage.getFloat("confidence");
			String contactId = mapMessage.getString("contact_id");
			String searchResult = mapMessage.getString("search_result");
			String receiveTime = mapMessage.getString("receive_time");
			
			//写快照表
			FaceSnapshot record = new FaceSnapshot();
			record.setSnapId(snapId);
			record.setIpcId(ipcId);
			record.setFaceImage(faceImage);
			record.setFullImage(fullImage);
			record.setFaceCount(faceCount);
			record.setSnapTime(snapTime);
			record.setMsec(msec);
			record.setImageId(imageId);
			record.setTargetFacetoken(targetFacetoken);
			record.setFaceToken(faceToken);
			record.setImageUuid(imageMd5);
			record.setConfidence(BigDecimal.valueOf(confidence));
			record.setContactId(contactId);
			record.setSearchResult(searchResult);
			record.setReceiveTime(receiveTime);
			faceSnapshotMapper.insert(record);
			
//			Customer customer = new Customer();
//			customer.setContactId(contactId);
//			customer.setHeadImage(imageMd5);
			//判断是否是Vip
			int isVip = customerMapper.findVipByContactId(contactId);
			
			if(isVip>0)
			{
				//写通知表
				FaceNotice notice = new FaceNotice();
				notice.setContactId(contactId);
				notice.setImageUuid(imageMd5);
				notice.setMsec(msec);
	//			notice.setNoticeId(noticeId);
	//			notice.setFaceId(faceId);
				try {
					DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					notice.setNoticeDatetime(dateFormat.parse(snapTime));
				} catch (ParseException e) {
					e.printStackTrace();
				}
	//			notice.setNoticeStatus();
	//			notice.setNoticeTitle();
				faceNoticeMapper.insert(notice);
			}
			//写事件表
			EventLog eventLog = new EventLog();
			eventLog.setContactId(contactId);
			eventLog.setEventType("1");//1:进店,2:购买
			eventLog.setEventTimestamp(snapTime);
			eventLogMapper.insert(eventLog);
			//写到店事件记录
			EventStorelog storeLog = new EventStorelog();
			int eventId = eventLog.getEventId();
			if(eventId>0) {
				storeLog.setContactId(contactId);
				storeLog.setEventId(eventId);
				storeLog.setHeadImage(imageMd5);
				storeLog.setSnapTime(snapTime);
				storeLog.setStoreName("万博欧时力店");
				eventStorelogMapper.insert(storeLog);
			}
			//更新顾客最近到店时间
			if(null!=contactId && !"".equals(contactId))
				customerMapper.updateLastDateArrived(snapTime, contactId);
			//p4:begin
			File f = new File(webappRoot);
			String webappParent = f.getParent();
			StringBuffer path = new StringBuffer(webappParent)
					.append(File.separator).append("face")
					.append(File.separator).append("pic")
					.append(File.separator).append(imageMd5).append(".jpg");
			ImageFileUtil.byte2image(faceImage, path.toString());//生成人脸头像图片
			//p4:end
			
			
		} catch (JMSException e) {
			e.printStackTrace();
			return;
		}

	}

	/* (non-Javadoc)
	 * @see com.ddyx.service.base.BaseNodifyService#doMessage(javax.jms.TextMessage)
	 */
	@Override
	public void doMessage(TextMessage textMessage) {
		// TODO Auto-generated method stub

	}
	@Override
	public String getQueueName() {
		
		return "DDSearchFace";
	}

}
